class ListingsController < ApplicationController
  def index_university
    @category = Category.find_by_stripped_name(params[:category])
    id = @category.sub_categories
    @university = University.find_by_stripped_name(params[:university])
    @advertisments = @category.advertisments.paginate(:page =>params[:page],:per_page => 10,:order=>"created_at desc",:conditions =>{ :sub_category_id => id , :university_id => @university.id})
    @total_ads= @advertisments.total_entries

    #       if @featured_advertisments
    #       @attachment=Attachment.find_by_advertisment_id(@featured_advertisments)
    #       end
    #       if @attachment
    #       @image=@attachment.file
    #       else
    #         @image=''
    #       end
    @sub_category=SubCategory.find_by_category_id(@category.id)
    @sub_category_fields=@sub_category.advertisment_form_field
    # @attribute_fields=@sub_category.advertisments.advertisment_attribute_field_values.find(:all)
    @attribute_fields=AdvertismentAttributeFieldValue.find_by_advertisment_id(@advertisments)
    # @attribute_fields=@category.advertisments.advertisment_attribute_field_values.find(:all)

    respond_to do |format|
      format.html
      format.js {
        render :update do |page|
          page.replace_html 'listings-search-results', :partial => 'advertisments'
        end
      }
    end
  end

  def index_city
    
  end


  def search

          @original_category=params[:category]
          @original_sub_category=(params[:sub_category])
          @original_university = (params[:university])
          @location=params[:location]
          search_term = params[:keyword]
          @search_term=search_term
          @category = Category.find_by_stripped_name(params[:category])
          @sub_category=SubCategory.find_by_stripped_name(params[:sub_category])
          @university = University.find_by_stripped_name(params[:university])
          if (params[:keyword])!=''
            search_term = search_term.split(" ").collect{|x| "title like '%#{x}%' or description like '%#{x}%' "}.join(" or ")
            @search_term1 = "("+ search_term + ") and (sub_category_id="+@sub_category.id.to_s+" and university_id="+@university.id.to_s+"  and location like '%"+@location.to_s+"%')"
          else
            @search_term1 = "sub_category_id="+@sub_category.id.to_s+" and university_id="+@university.id.to_s+"  and location like '%"+@location.to_s+"%'"
          end
          @advertisments =Advertisment.paginate :page => params[:page],:per_page => 10,:conditions =>@search_term1
          @total_ads=Advertisment.count(:conditions =>@search_term1)
              respond_to do |format|
                format.html
                format.js {
                    render :update do |page|
                    page.replace_html 'listings-search-results', :partial => 'search_results'
                    end
                 }
              end
end

def filtered_advertisments

       @filtered_advertisments= params[:advertisments].split("-")
       @category = Category.find_by_stripped_name(params[:category])
       id = @category.sub_categories
       @university = University.find_by_stripped_name(params[:university])
       @advertisments = Advertisment.find(@filtered_advertisments).paginate(:page =>params[:page],:per_page => 10,:order=>"created_at desc",:conditions =>{ :sub_category_id => id , :university_id => @university.id})
       @total_ads=@advertisments.total_entries
         
#       if @featured_advertisments
#       @attachment=Attachment.find_by_advertisment_id(@featured_advertisments)
#       end
#       if @attachment
#       @image=@attachment.file
#       else
#         @image=''
#       end
       @sub_category=SubCategory.find_by_category_id(@category.id)
       @sub_category_fields=@sub_category.advertisment_form_field
      # @attribute_fields=@sub_category.advertisments.advertisment_attribute_field_values.find(:all)
       @attribute_fields=AdvertismentAttributeFieldValue.find_by_advertisment_id(@advertisments)
      # @attribute_fields=@category.advertisments.advertisment_attribute_field_values.find(:all)

        respond_to do |format|
        format.html
        format.js {
                      render :update do |page|
                      page.replace_html 'listings-search-results', :partial => 'advertisments'
                      end
                  }
         end

end

def save_advertisment
       @advertisment           = Advertisment.find(params[:id])
          if session[:user]
             @user_id                = session[:user][:id]
             @bookmarks          = BookmarkedAd.find(:all, :conditions=>"user_id='"+@user_id.to_s+"'")
          end
           render :layout => false
end

def insert_ads
      @advertisments = Advertisment.find(:all,:limit => 10)
      @tags = Tag.find(:all)
      @tagcloud = Hash.new
      @tags.each do |t|
              @result=t.name.split(",")
              @result.each do |tags|
              @tagcloud[tags]
                    if(@tagcloud[tags].nil?)
                              @tagcloud[tags]=1
                    else
                              @tagcloud[tags]=@tagcloud[tags]+1
                    end
               end
      end
      @maxsize = 40;
      @minsize = 10;
      @maxval = @tagcloud.values.max
      @minval = @tagcloud.values.min
      @spread = @maxval - @minval;
      @step = ((@maxsize - @minsize)*15) / (@spread);
end

def bookmark_ad
      @user_id = session[:user][:id]
      @advertisment = Advertisment.find_by_id(params[:id])
      @folder = params[:bookmark][:folder]
      #@event_id = params[:event]
      if request.post?
          @bookmark = BookmarkedAd.new(:folder=>@folder,:user_id=>@user_id,:advertisment_id=>@advertisment.id)
          @bookmark.save
          redirect_to :action=>"show", :id=>params[:id]
          flash[:message]="Advertisment Saved Successfully"
      end


end
def invite_friends
#@user = User.find(params[:id])
end

def import
#@users = User.find(params[:id])
begin
@sites = {"gmail"  => Contacts::Gmail, "yahoo" => Contacts::Yahoo, "hotmail" => Contacts::Hotmail}
@contacts = @sites[params[:from]].new(params[:login], params[:password]).contacts
#@users , @no_users = [], []
#@contacts.each do |contact|
#if u = User.find(:first , :conditions => @users.email = ‘#{contact[1]}’ , :include =>[:user])
#if u = User.find(:first , :conditions => "email = '#{contact[1]}'" )
#@users << u
#else
#@no_users << {:name => contact[0] , :email => contact[1]}
#end
#end
respond_to do |format|
format.html {render :template => 'shared/_contact_list', :layout => false}
format.xml {render :xml => @contacts.to_xml}
end
end

end
end
